package com.akn.mvvmlibrary.utils

import android.content.Context
import android.graphics.Point
import android.view.View
import android.view.WindowManager
import com.blankj.utilcode.util.PermissionUtils
import com.blankj.utilcode.util.RomUtils
import com.blankj.utilcode.util.ToastUtils
import java.text.SimpleDateFormat
import java.util.*


object AppUtil {
    fun requestPermission(
        permissions: String,
        permissionOK: () -> Unit,
    ) {
        val isStorage = PermissionUtils.isGranted(permissions)
        if (isStorage) {
            permissionOK()
        } else {
            PermissionUtils.permission(permissions).callback(object :
                PermissionUtils.FullCallback {
                override fun onGranted(granted: MutableList<String>) {
                    permissionOK()
                }

                override fun onDenied(
                    deniedForever: MutableList<String>,
                    denied: MutableList<String>,
                ) {
                    ToastUtils.showShort("在设置-应用设置中开启相关权限，以正常使用app")
                }

            }).request()
        }

    }

    /**
     * 获取view的位置坐标
     */
    fun getLoc(view: View): IntArray {
        val location = IntArray(2)
        view.getLocationOnScreen(location)
        return location
    }


    fun getScreenSize(context: Context): Point {
        val windowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
        val display = windowManager.defaultDisplay
        val point = Point()
        display.getSize(point)
        return point
    }


    /*时间戳转换成字符窜*/
    fun getDateToString(time: Long, pattern: String = "yyyy/MM/dd HH:mm"): String {
        if (time <= 0)
            return ""
        val d = Date(time)
        val sf = SimpleDateFormat(pattern)
        return sf.format(d)
    }

    /*时间戳转换成字符窜*/
    fun getDateToString(time: Long): String {
        if (time <= 0)
            return ""
        val d = Date(time)
        val sf = SimpleDateFormat("yyyy/MM/dd HH:mm")
        return sf.format(d)
    }

    /**获取系统代码*/
    fun getSystemCode(): Int {
        if (RomUtils.isXiaomi())
            return 3
        if (RomUtils.isHuawei())
            return 4
        return 2
    }

    /**
     * 注册平台 1.TPNS  3.小米  4.华为
     */
    fun getProviderID(): Int {
//        if (RomUtils.isXiaomi())
//            return 3
//        if (RomUtils.isHuawei())
//            return 4
        return 1
    }
}